package com.jzhao.gper.combination;

import java.util.ArrayList;
import java.util.List;

/**
 * 树干，都有分支，也就是组合当中的整体
 */
public class Trunk extends Tree{
    private long level;

    private List<Tree> subTrees = new ArrayList<Tree>();

    /**
     * 树干有层级，提供设置层级的构造方法
     * @param xpath
     * @param level
     */
    public Trunk(String xpath,long level) {
        this.xpath = xpath;
        this.level = level;
    }

    /**
     * 添加一个分支
     * @param tree
     */
    public void addSub(Tree tree){
        this.subTrees.add(tree);
    }

    public String getPath() {
       return this.xpath;
    }

    /**
     * 获取当前树干下树路径
     * @return
     */
    public void getFullPath(){
        System.out.println(this.xpath);
        if(this.subTrees.size()>0){
            for (Tree subTree : this.subTrees) {
                //子级统一再缩进4个空格
                String IndentationDepth ="";
                for (int i = 0; i < this.level; i++) {
                    IndentationDepth += "    ";
                }
                System.out.print(IndentationDepth);
                if(subTree instanceof Branch){
                    System.out.println("+-"+subTree.getPath());
                }
                else if(subTree instanceof Trunk){
                    System.out.print("+-");
                    ((Trunk)subTree).getFullPath();
                }
            }
        }
    }

}
